package com.xy.tianyu.module.picture.fragment

import com.blankj.utilcode.util.ScreenUtils
import com.bumptech.glide.Glide
import com.xy.tianyu.LazyLoadBaseFragment
import com.xy.tianyu.R
import com.xy.tianyu.module.picture.bean.Item
import com.xy.tianyu.module.picture.bean.ItemX
import com.xy.tianyu.module.picture.bean.PicSearchBean
import com.xy.tianyu.utils.GlideUtils
import kotlinx.android.synthetic.main.fragment_pic_hot_day_detail.*

/**
 * @time 2020/10/13 18:05
 * @class 描述:
 */
class PicDetailFragment : LazyLoadBaseFragment() {
    companion object{
        const val BUNDLE_TYPE = "bundle_type"
        const val BUNDLE_PIC_DETAIL = "bundle_pic_Detail"
    }
    override fun setContentView(): Int {
        return R.layout.fragment_pic_hot_day_detail
    }

    override fun lazyLoad() {
        when(arguments?.get(BUNDLE_TYPE) as Int){
            0 -> {
                val picHotDetailItem = arguments?.get(BUNDLE_PIC_DETAIL) as Item
                if (picHotDetailItem != null){
                    val picAttrs = picHotDetailItem.pic_attrs
                    val params = iv_hot_detail.layoutParams
                    params.width = ScreenUtils.getScreenWidth()
                    var height = 0
                    val thumbWidth = picAttrs.thumb_width
                    val thumbHeight = picAttrs.thumb_height
                    height = when{
                        thumbWidth > thumbHeight -> ScreenUtils.getScreenWidth()/2
                        thumbWidth < thumbHeight -> ScreenUtils.getScreenWidth()*2
                        else -> ScreenUtils.getScreenWidth()
                    }
                    params.height = height
                    iv_hot_detail.layoutParams = params
                    Glide.with(this).load(picAttrs.thumb_url).apply(GlideUtils.getRequestOptions()).into(iv_hot_detail)
                    tv_pic_source.text = "来源：${picAttrs.from_url}"
                    tv_pic_hot_title.text = picAttrs.title
                }
            }
            1 -> {
                val picDetailItem = arguments?.getSerializable(BUNDLE_PIC_DETAIL) as PicSearchBean.DataBean
                if (picDetailItem != null){
                    val params = iv_hot_detail.layoutParams
                    params.width = ScreenUtils.getScreenWidth()
                    var height = 0
                    val thumbWidth = picDetailItem.width
                    val thumbHeight = picDetailItem.height
                    height = when{
                        thumbWidth > thumbHeight -> ScreenUtils.getScreenWidth()/2
                        thumbWidth < thumbHeight -> ScreenUtils.getScreenWidth()*2
                        else -> ScreenUtils.getScreenWidth()
                    }
                    params.height = height
                    iv_hot_detail.layoutParams = params
                    Glide.with(this).load(picDetailItem.thumbURL).apply(GlideUtils.getRequestOptions()).into(iv_hot_detail)
                    tv_pic_source.text = "来源：${picDetailItem.fromURL}"
                    tv_pic_hot_title.text = picDetailItem.fromPageTitleEnc
                }
            }
            else -> {
                val picHotDetailItemX = arguments?.get(BUNDLE_PIC_DETAIL) as ItemX
                if (picHotDetailItemX != null){
                    val picAttrs = picHotDetailItemX.pic_attrs
                    val params = iv_hot_detail.layoutParams
                    params.width = ScreenUtils.getScreenWidth()
                    var height = 0
                    val thumbWidth = picAttrs.thumb_width
                    val thumbHeight = picAttrs.thumb_height
                    height = when{
                        thumbWidth > thumbHeight -> ScreenUtils.getScreenWidth()/2
                        thumbWidth < thumbHeight -> ScreenUtils.getScreenWidth()*2
                        else -> ScreenUtils.getScreenWidth()
                    }
                    params.height = height
                    iv_hot_detail.layoutParams = params
                    Glide.with(this).load(picAttrs.thumb_url).apply(GlideUtils.getRequestOptions()).into(iv_hot_detail)
                    tv_pic_source.text = "来源：${picAttrs.from_url}"
                    tv_pic_hot_title.text = picAttrs.title
                }
            }
        }

    }

    override fun stopLoad() {

    }
}